package com.stormrage.edu.serv.edu.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.edu.EduChapterDao;
import com.stormrage.edu.dao.edu.view.EduChapterVideoDao;
import com.stormrage.edu.dao.edu.view.EduCourseChapterDao;
import com.stormrage.edu.serv.edu.EduChapterServ;
import com.stormrage.edu.serv.edu.view.EduChapterVideoServ;
import com.stormrage.entity.edu.EduChapter;
import com.stormrage.entity.edu.rep.EduChapterRep;
import com.stormrage.entity.edu.req.EduChapterReq;
import com.stormrage.entity.rep.ListRep;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: EduChapterServImpl.java
 * @Package com.stormrage.edu.serv.edu.impl
 * @Description: 章节 Service
 * @author www.stormragetech.com
 * @date 2016年4月8日
 * @version V1.0
 */
@Service
public class EduChapterServImpl implements EduChapterServ {

	private static final Logger logger = LoggerFactory.getLogger(EduMajorServImpl.class);

	@Autowired
	private EduChapterDao eduChapterDao;

	@Autowired
	private EduChapterVideoDao eduChapterVideoDao;

	@Autowired
	private EduCourseChapterDao eduCourseChapterDao;

	@Autowired
	private EduChapterVideoServ eduChapterVideoServ;

	@Override
	public Integer create(final EduChapter arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 Create 】- 开始运行 ");

		Integer index = eduChapterDao.create(arg0);

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void update(final EduChapter arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 Update 】- 开始运行 ");

		eduChapterDao.update(arg0);

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 Update 】- 完成运行 ");
	}

	@Override
	public void delete(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 Delete 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		eduChapterDao.delete(map);
		eduChapterVideoDao.deleteByChapterId(map);
		eduCourseChapterDao.deleteByChapterId(map);

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 Delete 】- 完成运行 ");
	}

	@Override
	public Integer queryCount(final EduChapterReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryCount 】- 开始运行 ");

		Map<String, Object> map = initQuery(arg0);

		Integer index = eduChapterDao.queryCount(map);

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryCount 】- 完成运行 ");
		return index;
	}

	@Override
	public EduChapter queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryById 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EduChapter entity = eduChapterDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryById 】- 完成运行 ");
		return entity;
	}

	@Override
	public ListRep queryList(final Integer arg0, final EduChapterReq arg1) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduChapter> array = eduChapterDao.queryList();
		array.removeAll(queryListById(arg0));

		Iterator<EduChapter> it = array.iterator();
		while (it.hasNext()) {
			EduChapter chap = it.next();
			if (null != arg1.getChapter() && !arg1.getChapter().equals("")
					&& !chap.getChapter().contains(arg1.getChapter()))
				it.remove();
		}

		List<EduChapterRep> list = new ArrayList<EduChapterRep>();
		int seq = arg1.getPage().getStartPage() + 1;

		for (int i = 0; i < array.size(); i++) {
			if (i >= arg1.getPage().getStartPage() && i <= arg1.getPage().getEndPage()) {
				EduChapter chapter = array.get(i);
				Integer count = eduChapterVideoServ.queryCountById(chapter.getId());
				EduChapterRep chapRep = EduChapterRep.instance(chapter, seq, count);
				list.add(chapRep);
				seq++;
			}
		}

		ListRep rep = ListRep.instance(array.size(), list);

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryList 】- 完成运行 ");
		return rep;
	}

	@Override
	public ListRep queryList(final EduChapterReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = initQuery(arg0);

		Integer count = eduChapterDao.queryCount(map);

		List<EduChapter> array = eduChapterDao.queryList(map);

		List<EduChapterRep> list = new ArrayList<EduChapterRep>();
		int seq = 1;

		Iterator<EduChapter> it = array.iterator();
		while (it.hasNext()) {
			EduChapter chapter = it.next();
			Integer chapCount = eduChapterVideoServ.queryCountById(chapter.getId());
			EduChapterRep chapRep = EduChapterRep.instance(chapter, seq, chapCount);
			list.add(chapRep);
			seq++;
		}

		ListRep rep = ListRep.instance(count, list);

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryList 】- 完成运行 ");
		return rep;
	}

	@Override
	public List<EduChapter> queryListById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduChapter> array = eduChapterDao.queryListById(map);

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduChapterRep> queryRepListById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduChapterRep> array = eduChapterDao.queryRepListById(map);
		int seq = 1;

		Iterator<EduChapterRep> it = array.iterator();
		while (it.hasNext()) {
			EduChapterRep chapRep = it.next();
			Integer count = eduChapterVideoServ.queryCountById(chapRep.getViewId());
			chapRep.setSeq(seq);
			chapRep.setCount(count);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	private Map<String, Object> initQuery(EduChapterReq arg0) {

		if (null == arg0)
			return null;

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startPage", arg0.getPage().getStartPage());
		map.put("size", arg0.getPage().getSize());

		StringBuffer query = new StringBuffer("");

		if (null != arg0.getChapter() && arg0.getChapter().length() > 0) {
			query.append("chapter LIKE :chapter AND ");
			map.put("chapter", "%" + arg0.getChapter() + "%");
		}

		String sql = query.toString();
		if (null != sql && sql.length() > 0)
			map.put("query", " WHERE " + sql.substring(0, sql.length() - 4));
		else
			map.put("query", "");

		return map;
	}

}
